home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cfengine-1.5.3 / src / chflags.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-04-06  |  6.0 KB  |  208 lines

  1. /* cfengine for GNU
  2.  
  3.         Copyright (C) 1995
  4.         Free Software Foundation, Inc.
  5.  
  6.    This file is part of GNU cfengine - written and maintained
  7.    by Mark Burgess, Dept of Computing and Engineering, Oslo College,
  8.    Dept. of Theoretical physics, University of Oslo
  9.  
  10.    This program is free software; you can redistribute it and/or modify it
  11.    under the terms of the GNU General Public License as published by the
  12.    Free Software Foundation; either version 2, or (at your option) any
  13.    later version.
  14.  
  15.    This program is distributed in the hope that it will be useful,
  16.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.    GNU General Public License for more details.
  19.  
  20.   You should have received a copy of the GNU General Public License
  21.   along with this program; if not, write to the Free Software
  22.   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
  23.  
  24. */
  25.  
  26.  
  27. #include "cf.defs.h"
  28. #include "cf.extern.h"
  29.  
  30.      /* This code was contributed by Andreas Klussman */
  31.  
  32. /***************************************************************/
  33. /* Flagstring toolkit                                          */
  34. /***************************************************************/
  35.  
  36. /* adapted from FreeBSD /usr/src/bin/ls/stat_flags.c */
  37.  
  38.  
  39. #define    TEST(a, b, f) {                            \
  40.     if (!memcmp(a, b, sizeof(b))) {                    \
  41.         if (clear) {                        \
  42.             if (minusmask)                    \
  43.                 *minusmask |= (f);            \
  44.         } else if (plusmask)                    \
  45.             *plusmask |= (f);                \
  46.         break;                            \
  47.     }                                \
  48. }
  49.  
  50. /***************************************************************/
  51.  
  52. ParseFlagString( flagstring,plusmask,minusmask)
  53.  
  54. char *flagstring;
  55. u_long *plusmask,*minusmask;
  56.  
  57. { char *sp, *next;
  58.   int clear;
  59.   u_long value;
  60.  
  61. value = 0;
  62.  
  63.  if (flagstring == NULL)
  64.     {
  65.     return;
  66.     }
  67.  
  68.  Debug1("ParseFlagString(%s)\n",flagstring);
  69.  
  70.  for ( sp = flagstring; sp && *sp; sp = next )
  71.     {
  72.     if ( next = strchr( sp, ',' ))
  73.        {
  74.        *next ++ = '\0';
  75.        }
  76.     
  77.     clear = 0;
  78.     
  79.     if ( sp[0] == 'n' && sp[1] == 'o' )
  80.        {
  81.        clear = 1;
  82.        sp += 2;
  83.        }
  84.     
  85.     switch ( *sp )
  86.        {
  87.        case 'a':
  88.        TEST(sp, "arch", SF_ARCHIVED);
  89.        TEST(sp, "archived", SF_ARCHIVED);
  90.        goto Error;
  91.        case 'd':
  92.        clear = !clear;
  93.        TEST(sp, "dump", UF_NODUMP);
  94.        goto Error;
  95.        case 'o':
  96.        TEST(sp, "opaque", UF_OPAQUE);
  97.        goto Error;
  98.        case 's':
  99.        TEST(sp, "sappnd", SF_APPEND);
  100.        TEST(sp, "sappend", SF_APPEND);
  101.        TEST(sp, "schg", SF_IMMUTABLE);
  102.        TEST(sp, "schange", SF_IMMUTABLE);
  103.        TEST(sp, "simmutable", SF_IMMUTABLE);
  104.        TEST(sp, "sunlnk", SF_NOUNLINK);
  105.        TEST(sp, "sunlink", SF_NOUNLINK);
  106.        goto Error;
  107.        case 'u':
  108.        TEST(sp, "uappnd", UF_APPEND);
  109.        TEST(sp, "uappend", UF_APPEND);
  110.        TEST(sp, "uchg", UF_IMMUTABLE);
  111.        TEST(sp, "uchange", UF_IMMUTABLE);
  112.        TEST(sp, "uimmutable", UF_IMMUTABLE);
  113.        TEST(sp, "uunlnk", UF_NOUNLINK);
  114.        TEST(sp, "uunlink", UF_NOUNLINK);
  115.        goto Error;
  116.        
  117.        case '0':
  118.        case '1':
  119.        case '2':
  120.        case '3':
  121.        case '4':
  122.        case '5':
  123.        case '6':
  124.        case '7':
  125.        sscanf(sp,"%o",&value);
  126.        *plusmask = value;
  127.        *minusmask = ~value & CHFLAGS_MASK;
  128.        break;
  129.        
  130.        case '\0':
  131.        break;
  132.        
  133.      Error:
  134.        default:
  135.        printf( "Invalid flag string '%s'\n", sp );
  136.        yyerror ("Invalid flag string");
  137.        return;
  138.        }
  139.     }
  140.  Debug1("ParseFlagString:[PLUS=%o][MINUS=%o]\n",*plusmask,*minusmask);
  141. }
  142.  
  143.  
  144. /*
  145. CHFLAGS(1)              FreeBSD General Commands Manual             CHFLAGS(1)
  146.  
  147. NAME
  148.      chflags - change file flags
  149.  
  150. SYNOPSIS
  151.      chflags [-R [-H | -L | -P]] flags file ...
  152.  
  153. DESCRIPTION
  154.      The chflags utility modifies the file flags of the listed files as speci-
  155.      fied by the flags operand.
  156.  
  157.      The options are as follows:
  158.  
  159.      -H      If the -R option is specified, symbolic links on the command line
  160.              are followed.  (Symbolic links encountered in the tree traversal
  161.              are not followed.)
  162.  
  163.      -L      If the -R option is specified, all symbolic links are followed.
  164.  
  165.      -P      If the -R option is specified, no symbolic links are followed.
  166.  
  167.      -R      Change the file flags for the file hierarchies rooted in the
  168.              files instead of just the files themselves.
  169.  
  170.      Flags are a comma separated list of keywords.  The following keywords are
  171.      currently defined:
  172.  
  173.            arch    set the archived flag (super-user only)
  174.            dump    set the dump flag
  175.            sappnd  set the system append-only flag (super-user only)
  176.            schg    set the system immutable flag (super-user only)
  177.            sunlnk  set the system undeletable flag (super-user only)
  178.            uappnd  set the user append-only flag (owner or super-user only)
  179.            uchg    set the user immutable flag (owner or super-user only)
  180.            uunlnk  set the user undeletable flag (owner or super-user only)
  181.            archived, sappend, schange, simmutable, uappend, uchange, uimmutable,
  182.            sunlink, uunlink
  183.                    aliases for the above
  184.  
  185.      Putting the letters ``no'' before an option causes the flag to be turned
  186.      off.  For example:
  187.  
  188.            nodump  the file should never be dumped
  189.  
  190.      Symbolic links do not have flags, so unless the -H or -L option is set,
  191.      chflags on a symbolic link always succeeds and has no effect.  The -H, -L
  192.      and -P options are ignored unless the -R option is specified.  In addi-
  193.      tion, these options override each other and the command's actions are de-
  194.      termined by the last one specified.
  195.  
  196.      You can use "ls -lo" to see the flags of existing files.
  197.  
  198.      The chflags utility exits 0 on success, and >0 if an error occurs.
  199.  
  200. SEE ALSO
  201.      ls(1),  chflags(2),  stat(2),  fts(3),  symlink(7)
  202.  
  203. HISTORY
  204.      The chflags command first appeared in 4.4BSD.
  205.  
  206. BSD                             March 31, 1994                               1
  207. */
  208.